package com.tech.algorithm;

import com.sun.deploy.panel.ITreeNode;

import java.util.Arrays;

/**
 * @Description:
 * @Author: zl
 * @Date: 2019/7/30 16:22
 */
class Solution {
    public static int maxSubArray(int[] nums) {
        int sum = 0;
        int maxSubSum = 0;//最大子串和
        int maxSubIndex = 0;//最大子串下标
        int maxSubBegin = 0;//最大子串开始下标
        for (int i = 0; i < nums.length; i++) {
            if(nums.length==1) {
                maxSubSum = nums[i];
            }
            sum = 0;
            for (int j = i; j < nums.length; j++) {
                sum += nums[j];
                if (maxSubSum < sum) {
                    maxSubSum = sum;
                } else {

                }

            }
        }
        return maxSubSum;
//        arrNew = new int[maxSubIndex - maxSubBegin + 1];
//        for (int i = 0; i < arrNew.length; i++) {
//            arrNew[i] = nums[maxSubBegin + i];
//        }
//        return arrNew;

    }

    public static void main(String[] args) {
        int[] nums = {-2,-1};
        System.out.println(maxSubArray(nums));
        int sum = 0;
        int maxSubSum = 0;//最大子串和
        int maxSubIndex = 0;//最大子串下标
        int maxSubBegin = 0;//最大子串开始下标
        for (int i = 0; i < nums.length; i++) {
            if(nums.length==1) {
                maxSubSum = nums[i];
            }
            sum = 0;
            for (int j = i; j < nums.length; j++) {
                sum += nums[j];
                if (maxSubSum < sum) {
                    maxSubSum = sum;
                    maxSubIndex = j;
                    maxSubBegin = i;
                }

            }
        }
        System.out.println(maxSubBegin + ":" + maxSubIndex + ":" + maxSubSum);
    }
}

//
//        int[] arrNew = new int[maxSubIndex - maxSubBegin + 1];
//        for (int i = 0; i < arrNew.length; i++) {
//            arrNew[i] = nums[maxSubBegin + i];
//        }
//
//        System.out.println(Arrays.toString(arrNew));

//
//    int[] nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
//    //System.out.println(maxSubArray(nums));
//    int sum = 0;
//    int maxSubSum = 0;//最大子串和
//    int maxSubIndex = 0;//最大子串下标
//    int maxSubBegin = 0;//最大子串开始下标
//        for (int i = 0; i < nums.length; i++) {
//        sum = 0;
//        for (int j = i; j < nums.length; j++) {
//        sum += nums[j];
//        if (maxSubSum < sum) {
//        maxSubSum = sum;
//        maxSubIndex = j;
//        maxSubBegin = i;
//        }
//
//        }
//        }
//        System.out.println(maxSubBegin + ":" + maxSubIndex + ":" + maxSubSum);
//        }